﻿using System;
using NUnit.Framework;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;
using ManualServiceRegistration.IoC;

namespace ManualServiceRegistration
{
  public class Program
  {
    [Test]
    public static void Main()
    {
      // Loading domain configuration section with name "Default".
      var domainConfiguration = DomainConfiguration.Load("Default");
      // Setting custom service container type for domain services
      domainConfiguration.ServiceContainerType = typeof (MyDomainServiceContainer);

      // Setting custom service container type for session services
      var sessionConfiguration = new SessionConfiguration("Default");
      sessionConfiguration.ServiceContainerType = typeof (MySessionServiceContainer);
      domainConfiguration.Sessions.Add(sessionConfiguration);

      var domain = Domain.Build(domainConfiguration);

      // Accessing domain service
      var domainService = domain.Services.Get<IMyDomainService>();
      Assert.IsNotNull(domainService);
      Console.WriteLine("Successfully obtained IMyDomainService through Domain.Services");

      // Accessing session service
      using (var session = domain.OpenSession()) {
        var sessionService = session.Services.Get<IMySessionService>();
        Assert.IsNotNull(sessionService);
        Console.WriteLine("Successfully obtained IMySessionService through Session.Services");
      }

      // Accessing domain service through session
      using (var session = domain.OpenSession()) {
        domainService = session.Services.Get<IMyDomainService>();
        Assert.IsNotNull(domainService);
        Console.WriteLine("Successfully obtained IMyDomainService through Session.Services");
      }
    }
  }
}
